package com.imooc.security.browser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * Created by zghgchao 2018/4/20 23:44
 */
@Component
public class MyUserDetailService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

//    @Autowired
//    private Mapper mapper;//可以在这里注入JPA/DAO对象，对数据库进行查询

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("登录用户名 : "+username);
        // 根据username 去数据库中查找用户信息
//        return new User(username,password,authorities);//参数从数据库中获取

        //根据查找到的用户信息判断用户是否被冻结

        // 在真实场景中，应该是从数据库中取到加密后的值，encode实在注册是用的
        String encodePassword = passwordEncoder.encode("123456");
        logger.info("数据库密码是："+encodePassword);

        return new User(username,encodePassword,
                true,true,true,true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
